/*
 * File: SeriesBean.java
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package tvseries.beans;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;

import tvseries.entity.Episode;
import tvseries.utils.DataUtil;
import tvseries.utils.FacesUtil;
import tvseries.utils.FileUtil;

/**
 * JSF interface bean class for tv series.
 * 
 * @author nish.naidoo@gmail.com
 * 
 */
public class SeriesBean implements Comparable<SeriesBean>
{
	private static final Logger log = Logger.getLogger(SeriesBean.class);

	private String id = null;
	private String title = null;
	private String imdbUrl = null;
	private String summary = null;
	private Date lastUpdate = null;
	private Set<EpisodeBean> episodes = null;

	private boolean selected = false;
	private boolean showArchived = false;
	private boolean showIgnored = false;
	private boolean showAvailable = true;
	private boolean showMissing = true;

	private String episodeStatus = null;

	public String changeStatus()
	{
		if (episodeStatus == null || "".equals(episodeStatus))
		{
			log.warn("Change status action for episodes had no status set to change to.");
			return ActionResult.FAILURE;
		}

		for (EpisodeBean episode : episodes)
		{
			if (!episode.isSelected())
				continue;

			episode.setSelected(false);

			if (episodeStatus.equals("Automatic"))
			{
				if (FileUtil.exists(episode))
					episode.setStatus("Available");
				else
					episode.setStatus("Missing");
			}
			else
			{
				Episode e = DataUtil.getEpisode(episode.getId());
				e.setStatus(episodeStatus);
				DataUtil.saveEpisode(e);

				episode.setStatus(episodeStatus);
			}
		}

		return ActionResult.SUCCESS;
	}

	public String selectAllEpisodes()
	{
		for (EpisodeBean e : episodes)
		{
			String status = e.getStatus();
			if (status == null || status.equals(""))
				e.setSelected(true);
			else if (showAvailable && status.equals("Available"))
				e.setSelected(true);
			else if (showMissing && status.equals("Missing"))
				e.setSelected(true);
			else if (showIgnored && status.equals("Ignored"))
				e.setSelected(true);
			else if (showArchived && status.equals("Archived"))
				e.setSelected(true);
		}

		return ActionResult.SUCCESS;
	}

	public String deselectAllEpisodes()
	{
		for (EpisodeBean e : episodes)
		{
			String status = e.getStatus();
			if (status == null || status.equals(""))
				e.setSelected(false);
			else if (showAvailable && status.equals("Available"))
				e.setSelected(false);
			else if (showMissing && status.equals("Missing"))
				e.setSelected(false);
			else if (showIgnored && status.equals("Ignored"))
				e.setSelected(false);
			else if (showArchived && status.equals("Archived"))
				e.setSelected(false);
		}

		return ActionResult.SUCCESS;
	}

	public List<EpisodeBean> getEpisodeList()
	{
		List<EpisodeBean> episodeList = new ArrayList<EpisodeBean>(episodes);
		if (episodeList.size() == 0)
			return episodeList;

		episodeList = new ArrayList<EpisodeBean>();
		for (EpisodeBean e : episodes)
		{
			String status = e.getStatus();
			if (status == null || status.equals(""))
				episodeList.add(e);
			else if (showAvailable && status.equals("Available"))
				episodeList.add(e);
			else if (showMissing && status.equals("Missing"))
				episodeList.add(e);
			else if (showIgnored && status.equals("Ignored"))
				episodeList.add(e);
			else if (showArchived && status.equals("Archived"))
				episodeList.add(e);
		}

		return episodeList;
	}

	private EpisodeBean getEpisode(String episodeId)
	{
		for (EpisodeBean e : episodes)
			if (e.getId().equals(episodeId))
				return e;

		Episode e = DataUtil.getEpisode(episodeId);

		try
		{
			EpisodeBean eb = new EpisodeBean();					
			BeanUtils.copyProperties(eb, e);
			
			SeriesBean sb = new SeriesBean();
			BeanUtils.copyProperties(sb, e.getSeries());
			eb.setSeriesBean(sb);
			
			return eb;
		}
		catch (Exception ex)
		{
			log.warn("Error copying beans.");
		}

		return null;
	}

	public String viewEpisode()
	{
		String episodeId = FacesUtil.getRequestParameter("episodeId");
		EpisodeBean episode = getEpisode(episodeId);
		if (episode == null)
		{
			log.error("Error viewing episode: " + episodeId);
			return ActionResult.ERROR;
		}

		FacesUtil.setBean("tvEpisode", episode);

		return ActionResult.SUCCESS;
	}

	/**
	 * @return the id
	 */
	public String getId()
	{
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(String id)
	{
		this.id = id;
	}

	/**
	 * @return the title
	 */
	public String getTitle()
	{
		return title;
	}

	/**
	 * @param title the title to set
	 */
	public void setTitle(String title)
	{
		this.title = title;
	}

	/**
	 * @return the imdbUrl
	 */
	public String getImdbUrl()
	{
		return imdbUrl;
	}

	/**
	 * @param imdbUrl the imdbUrl to set
	 */
	public void setImdbUrl(String imdbUrl)
	{
		this.imdbUrl = imdbUrl;
	}

	/**
	 * @return the summary
	 */
	public String getSummary()
	{
		return summary;
	}

	/**
	 * @param summary the summary to set
	 */
	public void setSummary(String summary)
	{
		this.summary = summary;
	}

	/**
	 * @return the lastUpdate
	 */
	public Date getLastUpdate()
	{
		return lastUpdate;
	}

	/**
	 * @param lastUpdate the lastUpdate to set
	 */
	public void setLastUpdate(Date lastUpdate)
	{
		this.lastUpdate = lastUpdate;
	}

	/**
	 * @return the episodes
	 */
	public Set<EpisodeBean> getEpisodes()
	{
		return episodes;
	}

	/**
	 * @param episodes the episodes to set
	 */
	public void setEpisodes(Set<EpisodeBean> episodes)
	{
		this.episodes = episodes;
	}

	/**
	 * @return the selected
	 */
	public boolean isSelected()
	{
		return selected;
	}

	/**
	 * @param selected the selected to set
	 */
	public void setSelected(boolean selected)
	{
		this.selected = selected;
	}

	/**
	 * @return the episodeStatus
	 */
	public String getEpisodeStatus()
	{
		return episodeStatus;
	}

	/**
	 * @param episodeStatus the episodeStatus to set
	 */
	public void setEpisodeStatus(String episodeStatus)
	{
		this.episodeStatus = episodeStatus;
	}

	/**
	 * @return the showArchived
	 */
	public boolean getShowArchived()
	{
		return showArchived;
	}

	/**
	 * @param showArchived the showArchived to set
	 */
	public void setShowArchived(boolean showArchived)
	{
		this.showArchived = showArchived;
	}

	/**
	 * @return the showIgnored
	 */
	public boolean getShowIgnored()
	{
		return showIgnored;
	}

	/**
	 * @param showIgnored the showIgnored to set
	 */
	public void setShowIgnored(boolean showIgnored)
	{
		this.showIgnored = showIgnored;
	}

	/**
	 * @return the showAvailable
	 */
	public boolean getShowAvailable()
	{
		return showAvailable;
	}

	/**
	 * @param showAvailable the showAvailable to set
	 */
	public void setShowAvailable(boolean showAvailable)
	{
		this.showAvailable = showAvailable;
	}

	/**
	 * @return the showMissing
	 */
	public boolean getShowMissing()
	{
		return showMissing;
	}

	/**
	 * @param showMissing the showMissing to set
	 */
	public void setShowMissing(boolean showMissing)
	{
		this.showMissing = showMissing;
	}

	public int compareTo(SeriesBean s)
	{
		return this.title.compareToIgnoreCase(s.getTitle());
	}
}
